package com.qeado.admin.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qeado.admin.common.BaseConstData;
import com.qeado.admin.model.AdminAccount;

public class AuthorityFilter implements Filter {
	/**
	 * 该类用来判断用户是否登录，未登录则转向登录页面。
	 * @author Richard
	 * @version 1.0 2010/11/30
	 */

	public void destroy() {
	}

	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest)request;
		String uri = httpRequest.getRequestURI();
		AdminAccount userInfo = (AdminAccount)httpRequest.getSession().getAttribute(BaseConstData.ADMIN_USER);
		if(userInfo == null && !uri.endsWith(BaseConstData.LOGIN_URI)){
			//((HttpServletResponse)response).sendRedirect(BaseConstData.REDIRECT_PAGE);
			PrintWriter out = response.getWriter();
		    out.println("<html>");  
		    out.println("<script>");  
		    out.println("window.open ('index.htm','_top')");  
		    out.println("</script>");  
		    out.println("</html>");
		} 
		else
			chain.doFilter(request, response);
	}

}
